<?php
/**
 * @title 236. 二叉树的最近公共祖先 - BFS
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($value) { $this->val = $value; }
 * }
 */

class Solution {
    /**
     * @param TreeNode $root
     * @param TreeNode $p
     * @param TreeNode $q
     * @return TreeNode
     */
    function lowestCommonAncestor($root, $p, $q) {
        /**
         * @since 2024-01-10 性能较差，层次遍历，每次都需要把路径数组传递，增加空间消耗，导致时间消耗增加
         *
         * @since 2024-01-10 每个节点值互不相同，建立映射关系，val=>node
         */
        $valNodeMap = [];

        /**
         * @since 2024-01-10 p和q父节点路径数组
         */
        $pPathArray = $qPathArray = [];

        /**
         * @since 2024-01-05 BFS广度遍历
         */
        $nodeArray = [
            [$root, []],
        ];
        while($nodeArray){
            /**
             * @since 2024-01-05 下次遍历点的集合
             */
            $nextNodeArray = [];

            /**
             * @since 2024-01-05 遍历当前的节点集合
             */
            foreach ($nodeArray as list($node, $pathArray)){
                /**
                 * @since 2024-01-10 当前节点的值
                 */
                $val = $node->val;

                /**
                 * @since 2024-01-10 映射关系
                 */
                $valNodeMap[$val] = $node;

                /**
                 * @since 2024-01-10 当前节点的父节点路径
                 */
                $pathArray[$val] = $val;

                /**
                 * @since 2024-01-10 是否找到p和q节点
                 */
                if($val == $p->val){
                    $pPathArray = $pathArray;
                } elseif($val == $q->val) {
                    $qPathArray = $pathArray;
                } else {}

                /**
                 * @since 2024-01-05 下次遍历的点
                 */
                if($node->left !== null){
                    $nextNodeArray[] = [$node->left, $pathArray];
                } else {}
                if($node->right !== null) {
                    $nextNodeArray[] = [$node->right, $pathArray];
                } else {}
            }

            /**
             * @since 2024-01-05 下次遍历
             */
            $nodeArray = $nextNodeArray;
            if($pPathArray AND $qPathArray){
                break;
            } else {}
        }

        /**
         * @since 2024-01-10 q的父节点一个一个pop，是否在p的父节点路径中
         */
        while($qPathArray){
            $val = array_pop($qPathArray);
            if(isset($pPathArray[$val])){
                return $valNodeMap[$val];
            } else {}
        }
    }
}



/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.10_11.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
